<?php
/**
 * 《新闻管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class News extends InfoUtil {
	public $tbl = Constant::tbl_news;

	public $type = array(
		'-' => '请选择',
		'1' => '公司新闻',
		'2' => '行业咨询',
		'3' => '参展信息',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '草稿',
		'2' => '发布',
		'3' => '暂停',
		'4' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'cn_title' => '标题',
		'pubdate' => '发布日期',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'info/news/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'pubdate') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['pubdate'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if (strtotime($row['createtime']) < strtotime("-8 hour")) {
			return false;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _where($request) {
		$email_s = $request['email_s'];

		$where = " WHERE 1=1";

		if (!empty($email_s)) {
			$where .= " AND email LIKE '%" . $email_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['type_t'] = $this->type[$row['type']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/news/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/news/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$type = $request['type'];
		$pubdate = $request['pubdate'];

		$cn_title = addslashes($request['cn_title']);
		$cn_content = addslashes($request['cn_content']);

		$en_title = addslashes($request['en_title']);
		$en_content = addslashes($request['en_content']);

		$jp_title = addslashes($request['jp_title']);
		$jp_content = addslashes($request['jp_content']);
		
		$fr_title = addslashes($request['fr_title']);
		$fr_content = addslashes($request['fr_content']);

		$emp = $_SESSION['id'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(type, cn_title, cn_content, en_title, en_content, jp_title, jp_content, fr_title, fr_content, pubdate, emp, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $type, $cn_title, $cn_content, $en_title, $en_content, $jp_title, $jp_content, $fr_title, $fr_content, $pubdate, $emp);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/news/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$type = $request['type'];
		$pubdate = $request['pubdate'];
		$status = $request['status'];

		$cn_title = addslashes($request['cn_title']);
		$cn_content = addslashes($request['cn_content']);

		$en_title = addslashes($request['en_title']);
		$en_content = addslashes($request['en_content']);

		$jp_title = addslashes($request['jp_title']);
		$jp_content = addslashes($request['jp_content']);
		
		$fr_title = addslashes($request['fr_title']);
		$fr_content = addslashes($request['fr_content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET type = '%s', cn_title = '%s', cn_content = '%s', en_title = '%s', en_content = '%s'";
		$fmt .= ", jp_title = '%s', jp_content = '%s', fr_title = '%s', fr_content = '%s', pubdate = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $type, $cn_title, $cn_content, $en_title, $en_content, $jp_title, $jp_content, $fr_title, $fr_content, $pubdate, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['type_t'] = $this->type[$row['type']];
		$row['emp_t'] = Name::emp($row['emp']);
		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/news/view');
		return true;
	}
}
?>
